package com.cgly.medical.service.impl;

import com.cgly.medical.service.DrugCompanyService;
import com.cgly.medical.domain.DrugCompany;
import com.cgly.medical.mapper.DrugCompanyMapper;
import com.cgly.medical.util.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class DrugCompanyServiceImpl implements DrugCompanyService {

    @Resource
    private DrugCompanyMapper drugCompanyMapper;

    @Override
    public Msg selectAllDrugCompany1() {
        List<DrugCompany> drugCompanyModels = drugCompanyMapper.selectAllDrugCompany();
        return Msg.success().data("drugCompanyModels",drugCompanyModels);
    }

    @Override
    public Msg selectAllDrugCompany(Integer page,Integer limit) {
        PageHelper.startPage(page,limit);
        List<DrugCompany> drugCompanyList = drugCompanyMapper.selectAllDrugCompany();
        if(drugCompanyList.size()==0){
            return Msg.fail().mess("查询公司信息错误");
        }
        PageInfo<DrugCompany> drugCompanies=new PageInfo<>(drugCompanyList,5);
        return Msg.success().data("drugCompanies",drugCompanies);
    }

    @Override
    public Msg selectOneDrugCompany(Long id) {
        DrugCompany drugCompany = drugCompanyMapper.selectOneDrugCompany(id);
        if(drugCompany==null){
            return Msg.fail().mess("查询公司信息错误");
        }
        return Msg.success().data("drugCompany",drugCompany);
    }

    @Override
    public Msg insertDrugCompany(DrugCompany drugCompany) {
        List<DrugCompany> drugCompanies = drugCompanyMapper.selectSomeDrugCompany(drugCompany.getCompanyName(), drugCompany.getCompanyPhone());
        if(drugCompanies.size()==0){
            Date date=new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sdf.format(date);
            drugCompany.setCreatetime(format);
            drugCompany.setUpdatetime(format);
            int row = drugCompanyMapper.insertDrugCompany(drugCompany);
            if(row==0){
                return Msg.fail().mess("添加公司信息错误");
            }else{
                return Msg.success().data("insertDrugCompanyNum",row);
            }
        }else{
            return Msg.fail().mess("所填公司信息已经被注册过");
        }
    }

    @Override
    public Msg  updateDrugCompany(DrugCompany drugCompany) {
        Date date=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        drugCompany.setUpdatetime(format);
        int row = drugCompanyMapper.updateDrugCompany(drugCompany);
        if(row==0){
            return Msg.fail().mess("更新公司信息错误");
        }
        return Msg.success().data("updateDrugCompanyNum",row);
    }

    @Override
    public Msg  deleteDrugCompany(String companyName) {
        int row = drugCompanyMapper.deleteDrugCompany(companyName);
        if(row==0){
            return Msg.fail().mess("删除公司信息错误");
        }
        return Msg.success().data("deleteDrugCompanyNum",row);
    }

    @Override
    public Msg getSomeDrugCompany(DrugCompany drugCompany) {
        List<DrugCompany> drugCompanyList=null;
        PageHelper.startPage(drugCompany.getPn(),drugCompany.getSize());
        if(drugCompany==null){
            drugCompanyList = drugCompanyMapper.selectAllDrugCompany();
        }else{
            drugCompanyList = drugCompanyMapper.selectSomeDrugCompany(drugCompany.getCompanyName(), drugCompany.getCompanyPhone());
        }
        if(drugCompanyList.size()==0){
            return Msg.fail().mess("查询公司信息错误");
        }
        PageInfo<DrugCompany> drugCompanies=new PageInfo<>(drugCompanyList,5);
        return Msg.success().data("drugCompanies",drugCompanies);
    }
}
